﻿@RenderPage("~/Views/Shared/leftMenu.cshtml")
<div id="content-wrapper" ng-controller="BannerAddCtrl">
    <ol class="breadcrumb">
        <li><a href="/Admin">首页</a></li>
        <li><a href="/Banner">广告列表</a></li>
        <li class="active">新增广告</li>
    </ol>
    <br />
    <form name="myForm" ng-submit="SubmitForm(myForm.$valid)" class="form-horizontal" role="form" novalidate>
        <div class="col-md-6 animated fadeInRight">

            <div class="form-group has-feedback"
                 ng-class="{'has-success':myForm.Title.$valid,'has-error':myForm.Title.$invalid&&myForm.Title.$dirty}">
                <label class="col-sm-2 control-label">广告名称</label>
                <div class="col-sm-10">
                    <input type="text"
                           name="Title"
                           id="Title"
                           ng-model="Entity.Title" required class="form-control" />
                    <span class="help-block">广告名称</span>
                    <span ng-if="myForm.Title.$valid&&!myForm.Title.$pristine" class="glyphicon glyphicon-ok form-control-feedback"></span>
                    <span ng-if="myForm.Title.$invalid&&!myForm.Title.$pristine" class="glyphicon glyphicon-remove form-control-feedback"></span>
                </div>
            </div>

            <div class="form-group has-feedback"
                 ng-class="{'has-success':myForm.Summary.$valid,'has-error':myForm.Summary.$invalid&&myForm.Summary.$dirty}">
                <label class="col-sm-2 control-label">广告描述</label>
                <div class="col-sm-10">
                    <input type="text"
                           name="Summary"
                           id="Summary"
                           ng-model="Entity.Summary" required class="form-control" />
                    <span class="help-block">广告描述</span>
                    <span ng-if="myForm.Summary.$valid&&!myForm.Summary.$pristine" class="glyphicon glyphicon-ok form-control-feedback"></span>
                    <span ng-if="myForm.Summary.$invalid&&!myForm.Summary.$pristine" class="glyphicon glyphicon-remove form-control-feedback"></span>
                </div>
            </div>

            <div class="form-group" ng-init="Photo_List=[]">
                <label class="col-sm-2 control-label">广告图片</label>
                <div class="col-sm-10" id="ImportDataForm">
                    <div id="SuperSlideShow">
                        <a id="UploadButton" href="" class="btn btn-default"><i class="icon_plus"></i> 选择图片</a>
                    </div>
                    <span class="help-block">广告图片将在首页显示。</span>
                </div>
                <div class="col-sm-offset-2 col-sm-10" ng-show="Photo_List.length>0" style="margin-top:15px;">
                    <div id="filelist"></div>
                    <div id="console"></div>
                    <div id="MechantLogos" class="carousel slide" data-ride="carousel" style="border:solid 1px #ccc; padding:20px;">
                        <ol class="carousel-indicators">
                            <li data-target="#MechantLogos" ng-repeat="d in Photo_List" data-slide-to="{{$index}}" ng-class="{active:$index==0}"></li>
                        </ol>
                        <div class="carousel-inner" role="listbox" style="text-align:center;">
                            <div class="item" ng-repeat="d in Photo_List" ng-class="{active:$index==0}">
                                <img class="img-circle" src="{{d}}" width="140" height="140" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>


            <div class="form-group has-feedback"
                 ng-class="{'has-success':myForm.LinkUrl.$valid,'has-error':myForm.LinkUrl.$invalid&&myForm.LinkUrl.$dirty}">
                <label class="col-sm-2 control-label">链接地址</label>
                <div class="col-sm-10">
                    <input type="text"
                           name="LinkUrl"
                           id="LinkUrl"
                           ng-model="Entity.LinkUrl" required class="form-control" />
                    <span class="help-block">点击后跳转的连接</span>
                    <span ng-if="myForm.LinkUrl.$valid&&!myForm.LinkUrl.$pristine" class="glyphicon glyphicon-ok form-control-feedback"></span>
                    <span ng-if="myForm.LinkUrl.$invalid&&!myForm.LinkUrl.$pristine" class="glyphicon glyphicon-remove form-control-feedback"></span>
                </div>
            </div>

            <div class="form-group has-feedback"
                 ng-class="{'has-success':myForm.IsShow.$valid,'has-error':myForm.IsShow.$invalid&&myForm.IsShow.$dirty}">
                <label class="col-sm-2 control-label">是否显示</label>
                <div class="col-sm-10" ng-init='IsShow_Prop=[{ID:1,Name:"显示",Desc:""},{ID:0,Name:"隐藏",Desc:""}]'>
                    <div class="btn-group btn-group-sm" role="group">
                        <a class="btn btn-default"
                           ng-repeat="d in IsShow_Prop"
                           ng-class="{active:Entity.IsShow==d.ID}"
                           ng-click="Entity.IsShow=d.ID">
                            {{d.Name}}
                        </a>
                        <input type="hidden" name="IsShow" ng-model="Entity.IsShow" required />
                    </div>
                    <span class="help-block animated fadeIn"
                          ng-if="Entity.IsShow==d.ID"
                          ng-repeat="d in IsShow_Prop">{{d.Desc}}</span>
                    <span class="help-block" ng-if="Entity.IsShow==null">该广告是否投放到前台</span>
                </div>
            </div>
            <div class="form-group has-feedback"
                 ng-class="{'has-success':myForm.ShowIndex.$valid,'has-error':myForm.ShowIndex.$invalid&&myForm.ShowIndex.$dirty}">
                <label class="col-sm-2 control-label">展示位置</label>
                <div class="col-sm-10">
                    <input type="text"
                           name="ShowIndex"
                           id="ShowIndex"
                           ng-model="Entity.ShowIndex" required class="form-control" />
                    <span class="help-block">前台显示的位置</span>
                    <span ng-if="myForm.ShowIndex.$valid&&!myForm.ShowIndex.$pristine" class="glyphicon glyphicon-ok form-control-feedback"></span>
                    <span ng-if="myForm.ShowIndex.$invalid&&!myForm.ShowIndex.$pristine" class="glyphicon glyphicon-remove form-control-feedback"></span>
                </div>
            </div>
        </div>
        <div class="col-md-6 animated fadeInLeft">
            <div class="form-group">
                <label for="Permission" class="col-sm-2 control-label">所属分类</label>
                <div class="col-sm-10">
                    <span class="help-block">&nbsp;当前广告对应的分类。</span>
                    <ul id="TypeTree" class="ztree" style="margin-top: 0; width: 180px;"></ul>
                </div>
            </div>
        </div>
        <submit-btn></submit-btn>
    </form>
</div>
<link href="/Scripts/ZTree/zTreeStyle.css" rel="stylesheet" />
<script src="/Scripts/ZTree/jquery.ztree.all-3.5.min.js"></script>
<script src="/Scripts/plupload/plupload.full.min.js"></script>
<script type="text/javascript">
    var ZTreeObject = null;
    function ZTree_Init(nodes) {
        var nodeArr = new Array();
        for (var i = 0; i < nodes.length; i++) {
            nodeArr.push({ id: nodes[i].ID, pId: nodes[i].PID, name: nodes[i].Name, open: false, nocheck: false });
        }
        ZTreeObject = $.fn.zTree.init($("#TypeTree"),
            {
                view: { selectedMulti: true },
                edit: { enable: false, editNameSelectAll: true },
                data: { simpleData: { enable: true } },
                check: { enable: true, chkboxType: { "Y": "ps", "N": "ps" } }
            }, nodeArr);

        ZTreeObject.expandAll(true);
    }
    angular.module("app").controller("BannerAddCtrl", function ($location, $scope, API_Banner)
    {
        $scope.OnSubmit = false;
        $scope.SubmitForm = function (isValid)
        {
            if (!isValid) { toastr.error('验证失败'); return; }
            if ($scope.Photo_List.length < 1) { toastr.error('请上传广告图片。'); return; }

            $scope.OnSubmit = true;
            var pmsIDs = new Array();
            var ckdNodes = ZTreeObject.getCheckedNodes(true);
            for (var i = 0; i < ckdNodes.length; i++) {
                pmsIDs.push(ckdNodes[i].id);
            }
            if (pmsIDs.length > 0) { $scope.Entity.TypeIDs = pmsIDs.join(","); }
            else { $scope.Entity.TypeIDs = ""; }

            $scope.Entity.ImageUrl = $scope.Photo_List[0];
            $scope.Entity.UserID = APP.User.ID;
            $scope.Entity.CreateDate = new Date();

            API_Banner.Put($scope.Entity).then(function (r)
            {
                $scope.OnSubmit = false;
                if(r.code==0)
                {
                    toastr.success('更新成功！');
                    window.document.location.href="/Banner";
                }
                else
                {
                    toastr.error(r.Msg);
                }
            });
        };

        var InitUpload = function () {
            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4',
                browse_button: 'UploadButton',
                multi_selection: true,
                container: document.getElementById('ImportDataForm'),
                url: '/api/Common/Upload',
                flash_swf_url: '/scripts/plupload/Moxie.swf',
                silverlight_xap_url: '/scripts/plupload/Moxie.xap',
                filters: {
                    max_file_size: '10mb',
                    mime_types: [
                         { title: "Image files", extensions: "jpg,gif,png" },
                    ]
                },
                init: {
                    PostInit: function () {
                        $('#filelist').html('');
                    },
                    FilesAdded: function (up, files) {
                        plupload.each(files, function (file) {
                            $('#filelist').append('<div id="' + file.id + '" class="alert alert-info"><button data-val="' + file.id + '" type="button" class="close">×</button>' + file.name + ' (' + plupload.formatSize(file.size) + ')<div class="progress"><div class="progress-bar progress-bar-success progress-bar-striped" style="width:0%;"></div></div></div>');
                        });
                        uploader.start();
                    },
                    UploadProgress: function (up, file) {
                        $("#" + file.id + " .progress-bar").css("width", file.percent + "%").html(file.percent + "%");
                        if (file.percent >= 100) {
                            $("#" + file.id + " .progress-bar").html('正在上传图片，请稍等...');
                        }
                    },
                    Error: function (up, err) {
                        $('#console').html('<div class="alert alert-danger"><button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>错误代码：' + err.code + '，错误信息：' + err.message + '</div');
                    },
                    UploadComplete: function (up, files) {
                        toastr.info('上传图片成功！');
                        uploader.files.splice(0, uploader.files.length);
                        $("#filelist").empty();
                    },
                    FileUploaded: function (up, files, res) {
                        $scope.Photo_List = new Array();
                        $scope.Photo_List.push(res.response.substr(1, res.response.length - 2));
                        $scope.$apply();
                    }
                }
            });
            uploader.init();
        }

        $(function(){
            ZTree_Init(@MvcHtmlString.Create(ViewBag.Types));
            $(".demoTip").popover({ placement: "top", trigger: "hover" });
            InitUpload();
        });

        API_Banner.Detail(@Html.ViewContext.RouteData.Values["id"]).then(function (r)
        {
            $scope.Entity = r;
            $scope.Photo_List = new Array();
            $scope.Photo_List.push(r.ImageUrl);

            ZTree_Init(@MvcHtmlString.Create(ViewBag.Types));

            var ckdIds = r.TypeIDs.split(",");
            if (ckdIds.length > 0) {
                for (var i = 0; i < ckdIds.length; i++) {
                    var node = ZTreeObject.getNodeByParam("id", ckdIds[i], null);
                    if (node == null) { continue; }
                    ZTreeObject.checkNode(node);
                }
            }
        });

        $scope.IHelp = function () {
            var intro = introJs();
            intro.setOptions({
                nextLabel: '下一步 <i class="arrow_carrot-right"></i>',
                prevLabel: ' <i class="arrow_carrot-left"></i> 上一步',
                skipLabel: '关闭',
                doneLabel: '关闭',
                showProgress: false,
                overlayOpacity: 0.4,
                steps: [
                  {
                      intro: "<p>1，新闻是抓取新闻源的定时任务。</p><p>2，新闻不会影响网站性能，由后台服务运行。</p><p>3，新闻会自动给自己打分，评估好坏。</p>"
                  },
                  {
                      intro: "<p>1，新闻抓取后新闻源将同步到的平台。</p><p>2，您可是设置抓取后直接自动同步，也可以手动点击同步按钮，或是单个编辑后同步到平台。</p>"
                  },
                  {
                      intro: '<p>最规则时需要查看XPath，用Chrome浏览器打开一个网页，F12，在弹出的小窗口中选中一个标签，右键，看到“copy XPath”了吧！</p>'
                  }
                ]
            });
            intro.start();
        };
    });
</script>